package com.atguigu.fruit.dao.base;

import org.junit.Test;

import java.sql.*;

/**
 * @author BruceLee
 * @create 2022-02-28 15:12
 */
public abstract class BaseDao {

//    @Test
//    public void test1(){
//        System.out.println(getConn());
//    }

    public final String DRIVER = "com.mysql.jdbc.Driver";
    public final String URL = "jdbc:mysql://localhost:3357/fruitdb?useUnicode=true&characterEncoding=utf-8&useSSL=false";
    public final String USER = "root";
    public final String PASSWORD = "123456";
    
    protected Connection conn;
    protected PreparedStatement psmt;
    protected ResultSet rs;
    
    //返回conn连接
    public Connection getConn(){
        
        try {
            Class.forName(DRIVER);
            return DriverManager.getConnection(URL, USER, PASSWORD);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        return null;
    }

    //执行关闭资源操作
    public void close(ResultSet rs,PreparedStatement psmt,Connection conn){
        try {
            if(rs != null){
                rs.close();
            }
            if(psmt != null){
                psmt.close();
            }
            //conn.isClosed()作用：检索连接对象是否关闭
            if(conn != null && !conn.isClosed()){
                conn.close();
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

    }
    
}
